package com.budwk.app.miniapp.services.impl;

import com.budwk.starter.common.page.PageUtil;
import com.budwk.starter.database.service.BaseServiceImpl;
import com.budwk.app.miniapp.models.WxContainer;
import com.budwk.app.miniapp.services.WxcontainerService;
import org.nutz.aop.interceptor.ioc.TransAop;
import org.nutz.dao.Cnd;
import org.nutz.dao.Dao;
import org.nutz.dao.Sqls;
import org.nutz.dao.sql.Sql;
import org.nutz.ioc.aop.Aop;
import org.nutz.ioc.loader.annotation.IocBean;
import org.nutz.lang.Strings;

@IocBean(args = {"refer:dao"})
public class WxcontainerServiceImpl extends BaseServiceImpl<WxContainer> implements WxcontainerService {
    public WxcontainerServiceImpl(Dao dao) {
        super(dao);
    }

    @Override
    @Aop(TransAop.READ_COMMITTED)
    public Boolean switchFillAllFlag(WxContainer wxContainer) {
        Sql sql = dao().execute(Sqls.create("UPDATE wx_container SET fill_flag = @fillFlag WHERE user_id = @userId AND del_flag = 0")
                        .setParam("fillFlag", wxContainer.getFillFlag())
                        .setParam("userId", wxContainer.getUserId()));
        return sql.getInt() > 0;
    }


    /**
     * 构造查询SQL语句
     */
    public Cnd querySqlBuild(WxContainer bean, String pageOrderName, String pageOrderBy) {
        Cnd cnd = Cnd.NEW();
        if (Strings.isNotBlank(pageOrderName) && Strings.isNotBlank(pageOrderBy)) {
            cnd.orderBy(pageOrderName, PageUtil.getOrder(pageOrderBy));
        }
        if (bean.getUserId() != null && Strings.isNotBlank(bean.getUserId().toString())) {
            cnd.and("userId", "=", bean.getUserId());
        }
        if (bean.getContainerType() != null && Strings.isNotBlank(bean.getContainerType().toString())) {
            cnd.and("containerType", "=", bean.getContainerType());
        }
        if (bean.getLongitude() != null && Strings.isNotBlank(bean.getLongitude().toString())) {
            cnd.and("longitude", "=", bean.getLongitude());
        }
        if (bean.getLatitude() != null && Strings.isNotBlank(bean.getLatitude().toString())) {
            cnd.and("latitude", "=", bean.getLatitude());
        }
        if (bean.getCapacity() != null && Strings.isNotBlank(bean.getCapacity().toString())) {
            cnd.and("capacity", "=", bean.getCapacity());
        }
        if (bean.getUsed() != null && Strings.isNotBlank(bean.getUsed().toString())) {
            cnd.and("used", "=", bean.getUsed());
        }
        if (bean.getRemain() != null && Strings.isNotBlank(bean.getRemain().toString())) {
            cnd.and("remain", "=", bean.getRemain());
        }
        if (bean.getName() != null && Strings.isNotBlank(bean.getName().toString())) {
            cnd.and("name", "=", bean.getName());
        }
        if (bean.getFillFlag() != null && Strings.isNotBlank(bean.getFillFlag().toString())) {
            cnd.and("fillFlag", "=", bean.getFillFlag());
        }
        if (bean.getCreateTime() != null && Strings.isNotBlank(bean.getCreateTime().toString())) {
            cnd.and("createTime", "=", bean.getCreateTime());
        }
        if (bean.getAddress() != null && Strings.isNotBlank(bean.getAddress().toString())) {
            cnd.and("address", "=", bean.getAddress());
        }
        if (bean.getDetailAddress() != null && Strings.isNotBlank(bean.getDetailAddress().toString())) {
            cnd.and("detailAddress", "=", bean.getDetailAddress());
        }
        if (bean.getStartCollect() != null && Strings.isNotBlank(bean.getStartCollect().toString())) {
            cnd.and("startCollect", "=", bean.getStartCollect());
        }
        if (bean.getFillTm() != null && Strings.isNotBlank(bean.getFillTm().toString())) {
            cnd.and("fillTm", "=", bean.getFillTm());
        }
        if (bean.getCurrentRain() != null && Strings.isNotBlank(bean.getCurrentRain().toString())) {
            cnd.and("currentRain", "=", bean.getCurrentRain());
        }
        return cnd;
    }
}
